<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Member;
use App\Models\Member as ModelsMember;
use App\Models\UserType;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Redis;

class MemberController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Member(), function (Grid $grid) {
            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('user-info-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('user-info-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('user-info-delete')) {
                $grid->disableDeleteButton();
            }

            // $grid->disableCreateButton();
            $grid->column('userid', '用户ID')->sortable();
            $grid->column('username')->copyable('复制');
            // $grid->column('wx.nickname', '微信昵称')->copyable('复制');
            $grid->avatar()->customImage();
            $grid->column('phone');
            $grid->column('add_time')->display(function ($time) {

                return date('Y-m-d H:i:s', $time);
            })->sortable();
            // $grid->column('doctor_status')->using(['0' => '未认证','1' => '已认证'])->label();
            // $grid->column('cert_type')->using(['1' => '医师执业证书','2' => '医师资格证书','3' => '医师职称证','4' => '工作证加身份证'])->label();
            $grid->model()->orderBy('userid', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                // $filter->equal('userid');
                $filter->like('username');
                $filter->like('phone');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        // $model = ModelsMember::with('wx');
        $model = new ModelsMember();
        return Show::make($id, $model, function (Show $show) {



            $show->field('userid', '用户ID');
            $show->field('username');
            // $show->field('password');
            // $show->field('nickname');
            // $show->field('wx.nickname', '微信昵称');
            // $show->field('wx.openid', 'OPENID');
            // $show->field('wx.unionid', 'UNIONID');
            // $show->field('realname');
            $show->avatar()->image();
            // $show->field('email');
            $show->field('phone');
            // $show->field('sex');
            $show->field('last_login_time')->as(function ($time) {
                return date('Y-m-d H:i:s', $time);
            });
            // $show->field('reg_ip');
            // $show->field('add_time')->as(function($time){
            //     return date('Y-m-d H:i:s',$time);
            // });
            $show->field('add_time')->as(function ($time) {
                return date('Y-m-d H:i:s', $time);
            });

            // $show->field('idCard');
            // $show->field('age');
            // $show->field('integral');
            // $show->field('credit');
            // $show->field('sign_num');
            // $show->field('money');
            $show->userTypeId()->as(function ($id) {
                $state = [0 => '未知', 1 => '医生', 2 => '护士', 3 => '医学生', 4 => '医药/医疗器械企业人员', 5 => '其他'];
                return $state[$id];
            })->label();
            // $show->field('birth');
            // $show->field('is_del');
            // $show->field('json')->jsonToArray();
            // $show->field('remarks');
            $show->field('doctor_status')->using(['未认证', '已申请', '已认证', '驳回'])->label();
            // $show->field('cert_type')->using(['0' => '无', '1' => '医师执业证书', '2' => '医师资格证书', '3' => '医师职称证', '4' => '工作证加身份证'])->label();
            $show->status('状态')->using(['1' => '显示', '2' => '禁用']);

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('user-info-update')) {
                $show->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // // @permissions
            if (!Admin::user()->can('user-info-delete')) {
                $show->disableDeleteButton();
            }
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ModelsMember(), function (Form $form) {
            $form->disableEditingCheck();
            $form->disableViewCheck();
            $form->display('userid');
            $form->text('username')->required();
            $id = $form->getKey();
            if ($id) {
                $form->password('password', trans('admin.password'))
                    ->minLength(5)
                    ->maxLength(20)
                    ->customFormat(function () {
                        return '';
                    });
            } else {
                $form->password('password', trans('admin.password'))
                    ->required()
                    ->minLength(5)
                    ->maxLength(20);
            }
            // $form->text('nickname')->required();
            // $form->text('realname')->required();
            $form->image('avatar')->autoUpload()->move('medical/member/avatar/' . date('Y/m'));
            // $form->text('email')->required();
            $form->text('phone')->required();
            $form->radio('sex')->options([0 => '男', 1 => '女'])->default(0);

            // $form->select('userTypeId', '用户身份')
            //     ->options(UserType::all()->pluck('name', 'id'))
            //     ->saving(function ($value) {
            //         // $category = implode(',',$value);
            //         return $value;
            //     });
            // $form->keyValue('json')->setKeyLabel('键名')->setValueLabel('键值')->saving(function ($v) {
            // return json_encode($v);
            // });

            // dd($form->password);
            $form->radio('status')->options([1 => '正常', 2 => '禁用'])->default(1);
            // $form->radio('identity', '用户角色')->options(['user' => '普通用户', 'sale' => '销售', 'admin' => '管理员', 'partner' => '合作伙伴', 'expert' => '专家'])->default('user');
        })->saving(function (Form $form) {
            if ($form->password && $form->model()->get('password') != $form->password) {
                $form->password = bcrypt($form->password);
            }

            if (!$form->password) {
                $form->deleteInput('password');
            }
        });
    }
}
